<template>
  <el-dialog
    :title="title"
    :visible.sync="open"
    width="70%"
    :before-close="handleClose"
    class="dialogClass"
  >
    <el-form ref="ruleForm" :model="ruleForm" label-width="80px">
      <el-row :gutter="20">
        <el-col :xl="8" :lg="8">
          <el-form-item label="购销商名称:" prop="gouxiaoshangmingcheng" label-width="100px">
            <el-input
              size="big"
              v-model="ruleForm.gouxiaoshangmingcheng"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :xl="8" :lg="8">
          <el-form-item
            label="购销商编号："
            prop="gouxiangshangbianhao"
            label-width="100px"
          >
            <el-input
              size="big"
              v-model="ruleForm.gouxiangshangbianhao"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :xl="8" :lg="8">
          <el-form-item
            label="购销商物资："
            prop="gouxiangshangwuzi"
            label-width="100px"
          >
            <el-input
              size="big"
              v-model="ruleForm.gouxiangshangwuzi"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :xl="8" :lg="8">
          <el-form-item label="联系人:" prop="lianxiren" label-width="100px">
            <el-input
              size="big"
              v-model="ruleForm.lianxiren"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :xl="8" :lg="8">
          <el-form-item label="联系电话:" prop="dianhua" label-width="100px">
            <el-input
              size="big"
              v-model="ruleForm.dianhua"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button
        type="primary"
        size="big"
        @click="onSubmit('ruleForm')"
        :loading="loading"
        >{{ submitButton }}</el-button
      >
      <el-button size="big" @click="resetForm('ruleForm')">重置</el-button>
      <el-button size="big" @click="close('ruleForm')">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  savebasicsettings,
  updatebasicsettings,
} from "@/apis/material/basicSettings.js";

export default {
  data() {
    return {
      // 提交按钮
      submitButton: "",
      // 控制弹窗
      open: false,
      // 控制按钮加载状态
      loading: false,
      // 弹窗标题
      title: "",
      // 表单项
      ruleForm: {
        gouxiaoshangmingcheng: "",
        gouxiangshangbianhao: "",
        dianhua: "",
        gouxiangshangwuzi: "",
        lianxiren: "",
      },
    };
  },
  methods: {
    // 控制弹窗
    show(title, row) {
      const _this = this;
      _this.title = title;
      _this.open = true;
      if (row) {
        _this.submitButton = "修改";
        _this.ruleForm = JSON.parse(JSON.stringify(row));
      } else {
        _this.submitButton = "确定登记";
      }
    },
    close(formName) {
      this.open = false;
      this.resetForm(formName);
    },
    handleClose(done) {
      const _this = this;
      _this
        .$confirm("系统可能不会保存您的更改", "确认关闭？")
        .then((_) => {
          done();
          _this.resetForm("ruleForm");
        })
        .catch((_) => {});
    },
    onSubmit(formName) {
      const _this = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (_this.title == "新增购销单位") {
            // console.log(_this.ruleForm);
            savebasicsettings(_this.ruleForm).then(function (res) {
              // console.log("res: ", res.code);
              if (res.code === 0) {
                _this.$alert(
                  _this.ruleForm.gouxiangshangbianhao + res.msg,
                  "提示",
                  {
                    confirmButtonText: "确定",
                    callback: (action) => {
                      _this.close("ruleForm");
                      _this.$parent.$parent.search();
                    },
                  }
                );
              }
            });
          } else {
            updatebasicsettings(_this.ruleForm).then(function (res) {
              // console.log("res: ", res.code);
              if (res.code === 0) {
                _this.$alert(
                  _this.ruleForm.gouxiangshangbianhao + res.msg,
                  "提示",
                  {
                    confirmButtonText: "确定",
                    callback: (action) => {
                      _this.close("ruleForm");
                      _this.$parent.$parent.search();
                    },
                  }
                );
              }
            });
          }
        }
      });
    },
    // 表单重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
</style>